package com.itheima.web.Servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

public class BaseServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //获取资源路径 uri = 虚拟目录+映射路径
            String uri = req.getRequestURI();
            //截取根目录下的路径     /add
            String methodName = uri.substring(uri.lastIndexOf("/") + 1);//  add
            //拿到sevlet字节码文件对象
            Class clazz = this.getClass();

            Method method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            //暴力反射
            method.setAccessible(true);
            //调用 method方法
            method.invoke(this,req,resp);

        }  catch (Exception e) {
            e.printStackTrace();
        }




      /*  switch (option){
            case "/add":
                add(req,resp);
                break;
            case "/delete":
                delete(req,resp);
                break;
            case "/upDate":
                upDate(req,resp);
                break;
            case "/findAll":
                findAll(req,resp);
                break;
        }
*/
    }

    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{

    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
    }

    protected void upDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req ,resp);
    }
}
